package com.bph.user.service.impl;

import com.bph.commons.pojo.Permission;
import com.bph.commons.pojo.Store;
import com.bph.commons.pojo.User;
import com.bph.commons.utils.CookieValue;
import com.bph.commons.utils.IsNumericZidai;
import com.bph.commons.vo.ResultVo;
import com.bph.user.mapper.UserMapper;
import com.bph.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    //根据邮箱密码登录
    @Override
    public ResultVo selectUserByPassword(User user){

        User user1 = userMapper.selectUserByPassword(user);
        if(user1 == null){
            return ResultVo.getFailVo("信息错误",2);
        }
        else {
            return ResultVo.getSuccessVo("登录成功", user1);
        }
    }

    //根据验证码登录
    @Override
    public ResultVo selectUserByCode(User user, String code, HttpServletRequest request){
        User user1 = userMapper.selectUserByMail(user);
        System.out.println(user1);
        if(!(user.getUser_mail().equals(CookieValue.getCookieValue(request, "user_mail")))
                || !(code.equals(CookieValue.getCookieValue(request, "selectUserByCode")))){
            return ResultVo.getFailVo("验证错误",2);
        }
        else if(user1== null){
            return ResultVo.getFailVo("信息错误",3);
        }
        else {
            return ResultVo.getSuccessVo("登录成功", user1);
        }
    }


    /**
     * 忘记密码  修改
     * @param user
     * @param code
     * @param request
     * @return
     */
    @Override
    public ResultVo updatePasswordByCode(User user, String code, HttpServletRequest request) {
        String new_code = CookieValue.getCookieValue(request,"updatePasswordByCode");
        String user_mail = CookieValue.getCookieValue(request,"user_mail");
        //先判断验证码，再根据影响行数判断
        if (code.equals(new_code) && user.getUser_mail().equals(user_mail)) {
            int ret = userMapper.updatePasswordByCode(user.getUser_mail(), user.getUser_password());
            if(ret == 1){
                return ResultVo.getSuccessVo("修改成功", null);
            }
            else{
                return ResultVo.getFailVo("修改失败", 2);
            }
        }else {
            return ResultVo.getFailVo("验证错误",3);
        }
    }

    //注册店长
    @Override
    public ResultVo insertUserByMail(User user, String code, HttpServletRequest request) {
//        CookieValue cookieValue = new CookieValue();
        String new_code = CookieValue.getCookieValue(request,"insertUserByCode");
        String mail = CookieValue.getCookieValue(request,"user_mail");
        System.out.println(new_code);
        System.out.println(mail);
        if (code.equals(new_code) && user.getUser_mail().equals(mail)){
            /**
             *用于判断邮箱是否存在
             */
            User user1 = userMapper.SelectUserByMail(user);
            if(user1 != null){
                return ResultVo.getFailVo("邮箱已存在", 3);
            }
            else {
                int ret2 = userMapper.insertStore();
                Store store = userMapper.selectNewStoreId();
                int ret = userMapper.insertManagerByMail(user.getUser_mail(),user.getUser_password(),store.getStore_id());
                User user2 = userMapper.selectUserIdByMail(user.getUser_mail());
                int ret3 = userMapper.insertUserRole(user2.getUser_id());
                System.out.println(ret);
                System.out.println(ret2);
                System.out.println(ret3);
                System.out.println(store.getStore_id());
                System.out.println(user2.getUser_id());
                if(ret == 1 && ret2 == 1 && ret3 == 1) {
                    return ResultVo.getSuccessVo("注册成功", null);
                }
                else {
                    return ResultVo.getFailVo("注册时插入语句失败", 4);
                }
            }
        }else {
            return ResultVo.getFailVo("验证错误", 2);
        }
    }

    /**
     * 获取用户权限
     * sql语句还在考虑
     * @param request
     * @return
     */
    @Override
    public ResultVo getPermissionByMail(HttpServletRequest request) {

        String user_mail = CookieValue.getCookieValue(request,"mail");

        List<Permission> list = userMapper.selectPermissionByMail(user_mail);
        System.out.println(list.get(0).getPermission_name());
        if (list.size() != 0){
            return ResultVo.getSuccessVo("查询成功", list);
        }else {
            return ResultVo.getFailVo("无权限", 2);
        }
    }

    /**
     *修改密码
     * @param user
     * @param new_password
     * @return
     */
    @Override
    public ResultVo updatePasswordByPassword(User user, String new_password) {

        int ret = userMapper.updatePasswordByMail(user.getUser_mail(),user.getUser_password(), new_password);
        System.out.println(ret);
        if(ret == 1){
            return ResultVo.getSuccessVo("成功修改密码", new_password);
        }
        else{
            return ResultVo.getFailVo("密码错误", 2);
        }
    }

    //获取用户信息
    @Override
    public ResultVo getUserByMail(HttpServletRequest request){
       User user1 = userMapper.getUserByMail(CookieValue.getCookieValue(request, "mail"));
        if(user1 == null){
            return ResultVo.getFailVo("获取用户信息失败",2);
        }
        else {
            return ResultVo.getSuccessVo("获取用户信息成功",user1);
        }
    }

    //修改个人信息
    @Override
    public ResultVo updateUserByMail(User user){
        int ret = userMapper.updateUserByMail(user);
        if(ret == 0){
            return ResultVo.getFailVo("修改个人信息失败",2);
        }
        else {
            return ResultVo.getSuccessVo("修改个人信息成功",user);
        }
    }

    //获取员工列表
    @Override
    public ResultVo getUserById(User user){
        List<User> userList = userMapper.getUserByStoreId(user);
        if(userList.size() == 0){
            return ResultVo.getFailVo("获取失败",2);
        }
        else {
            return ResultVo.getSuccessVo("获取成功", userList);
        }
    }

    /**
     * 获取用户列表
     * @param key
     * @param user_title
     * @param store_id
     * @param limit_index
     * @param limit_length
     * @return
     */
    @Override
    public ResultVo selectUserByKey(String key, String user_title, int store_id,int limit_index, int limit_length) {

        //获取用户数
        //获取用户数
        int user_length = userMapper.getUserLength(store_id);

        //未规定职位时user_title就模糊查询
        if (user_title.equals("")) {
            user_title = "%";
        }

        //调用公共类判断是否为数字
        if (IsNumericZidai.isNumericZidai(key)) {
            //用于提交最终数据
            HashMap<String, Object> map = new HashMap<>();
            //key为数字就为id
            int user_id = Integer.parseInt(key);
            System.out.println(user_id);
            //根据id和对应的uer_title查询
            List<User> list = userMapper.selectUserById(user_id, user_title, store_id);
//            User user = userMapper.selectUserById(user_id,user_title,store_id);
            if (list != null) {
                map.put("user_length", user_length);
                map.put("user_list", list);
                System.out.println(map);
                return ResultVo.getSuccessVo("查询成功", map);
            } else {
                return ResultVo.getFailVo("查询失败", 2);
            }
        } else {
            //用于提交最终数据
            HashMap<String, Object> map = new HashMap<>();
            String user_name = "%" + key + "%";
            System.out.println(user_name);
            //key不为数字就为user_name
            List<User> list = userMapper.selectUserByName(user_name, user_title, store_id, limit_index, limit_length);
            //不为空时正常返回
            if (!(list.isEmpty())) {
                map.put("user_length", user_length);
                map.put("user_list", list);
                System.out.println(map);
                return ResultVo.getSuccessVo("查询成功", map);
            } else {
                //为空时说明查不到了，就返回查询失败
                return ResultVo.getFailVo("查询失败", 2);
            }
        }
    }

        /**
         * 删除用户
         * @param user_id
         * @return
         */
    @Override
    public ResultVo deleteUserById(int user_id) {

        userMapper.deleteUserRoleById(user_id);
        userMapper.deleteUserPreferenceById(user_id);
        int ret1 = userMapper.deleteUserById(user_id);

        if (ret1 == 1){
            return ResultVo.getSuccessVo("删除成功",null);
        }else {
            return ResultVo.getFailVo("删除失败",2);
        }
    }

    @Override
    public ResultVo updateUserById(User user) {
        int ret = userMapper.updateUserById(user);
        if (ret == 1){
            User user1 = userMapper.getUserById(user);
            return ResultVo.getSuccessVo("修改成功",user1);
        }else {
            return ResultVo.getFailVo("修改失败",2);
        }
    }

    @Override
    public ResultVo insertEmployeeByMail(User user) {
        /**
         *用于判断邮箱是否存在
         */
        User user1 = userMapper.SelectUserByMail(user);
        if(user1 != null){
            return ResultVo.getFailVo("添加失败，邮箱已存在", 2);
        }
        else {
            //默认密码123456
            int ret = userMapper.insertEmployeeByMail(user);
            User user2 = userMapper.selectUserIdByMail(user.getUser_mail());
            System.out.println("插入user的id:"+user2.getUser_id());
            int ret2 = userMapper.insertEmployeeUserRole(user2.getUser_id());
            if(ret == 1 && ret2 == 1) {
                return ResultVo.getSuccessVo("添加成功", null);
            }
            else {
                return ResultVo.getFailVo("注册时插入语句失败", 3);
            }
        }
    }


}
